package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

final public class Action extends Delegate<IAction> {

    public Action(IAction delegate) {
        super(delegate);
    }

    public void invoke() throws Exception {
        for (IAction action : delegates) {
            action.invoke();
        }
    }

    public AsyncResult<Void> invokeAsync(@Nullable AsyncDelegateCallback callback) {
        ArrayList<FutureTask<Void>> tasks = new ArrayList<>();
        for (int i = 0; i < delegates.size(); i++) {
            tasks.add(delegates.get(i).invokeAsync(null));
        }

        waitTasksFinish(tasks, null, callback);

        //返回tasks的浅拷贝
        return new AsyncResult<>(tasks);
    }
}